package com.ruicar.afs.cloud.afscase.risk.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.risk.config.TortoiseLinkConfig;
import com.ruicar.afs.cloud.afscase.risk.vo.TortoiseLinkInfoVO;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @Description
 * @Author Peng Jinbo
 * @Date 2020/08/11 18:51
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/config/tortoise")
public class TortoiseConfigController {

    /**
     * Tortoise link config
     */
    private TortoiseLinkConfig tortoiseLinkConfig;
    /**
     * Case base info service
     */
    private CaseBaseInfoService caseBaseInfoService;

    /**
     * Author Peng Jinbo
     * Link url config response
     *
     * @param applyNo apply no
     * @return the response
     */
    @GetMapping("/link")
    public IResponse linkUrlConfig(@RequestParam("applyNo")String applyNo){
        CaseBaseInfo baseInfo;
        if(StrUtil.isBlank(applyNo)){
            return IResponse.fail("申请编号不可为空");
        }
        baseInfo = caseBaseInfoService.getOne(
                Wrappers.<CaseBaseInfo>lambdaQuery()
                    .eq(CaseBaseInfo::getApplyNo,applyNo)
        );
        if(Objects.isNull(baseInfo)){
            return IResponse.fail("未找到案件详情");
        }
        TortoiseLinkInfoVO linkInfoVO = TortoiseLinkInfoVO.builder()
                .beforeApplyDiscernUrl(String.format(tortoiseLinkConfig.getBeforeApplyDiscernUrl(),baseInfo.getId()))
                .outsideDataQueryUrl(String.format(tortoiseLinkConfig.getOutsideDataQueryUrl(),+baseInfo.getId()))
                .build();
        return IResponse.success(linkInfoVO);
    }

}
